package com.alex.statistics.controller;


import com.alex.statistics.method.clusterAnalysis.*;
import com.alex.statistics.pojo.Result;

import com.alex.statistics.pojo.request.clusterAnalysis.CorrelationAnalysisRequest;
import com.alex.statistics.pojo.request.clusterAnalysis.DensityClusteringRequest;
import com.alex.statistics.pojo.request.clusterAnalysis.HierarchicalClusteringRequest;
import com.alex.statistics.pojo.request.clusterAnalysis.KMeansRequest;
import com.alex.statistics.pojo.result.clusterAnalysis.CorrelationAnalysisResult;
import com.alex.statistics.pojo.result.clusterAnalysis.DensityClusteringResult;
import com.alex.statistics.pojo.result.clusterAnalysis.HierarchicalClusteringResult;
import com.alex.statistics.pojo.result.clusterAnalysis.KMeansResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "聚类与分类分析", description = "包含K-Means聚类、层次聚类、密度聚类和分析")
@RequestMapping("/group")
public class ClusterAnalysisController {

    @Autowired
    private KMeansService kMeansService;

    @Autowired
    private HierarchicalClusteringService hierarchicalClusteringService;

    @Autowired
    private DensityClusteringService densityClusteringService;

    @Autowired
    private CorrelationAnalysisService correlationAnalysisService;

    // ================== K-Means聚类 ==================
    @Operation(
            summary = "K-Means聚类分析",
            description = """
                    ### 算法说明
                    基于K均值算法对数据进行聚类：
                    - 自动计算最优K值（当k=0时）
                    - 支持自定义初始质心位置
                    """
    )
    @PostMapping("/kmeans")
    public Result<KMeansResult> kMeans(@RequestBody @Parameter(description = "K-Means聚类请求参数") KMeansRequest request) {
        KMeansResult result = kMeansService.cluster(request);
        return Result.ok(result);
    }

    // ================== 层次聚类 ==================
    @Operation(
            summary = "层次聚类分析",
            description = """
                    ### 算法说明
                    基于层次聚类算法对数据进行多层聚类：
                    - 支持凝聚式和分裂式两种方法
                    - 支持多种距离计算方式
                    
                    ### 使用建议
                    1. 对于大规模数据集，建议使用K-Means作为预聚类
                    2. 可通过指定distanceThreshold参数自动确定聚类数量"""
    )
    @PostMapping("/hierarchical")
    public Result<HierarchicalClusteringResult> hierarchicalClustering(@RequestBody @Parameter(description = "层次聚类请求参数") HierarchicalClusteringRequest request) {
        HierarchicalClusteringResult result = hierarchicalClusteringService.cluster(request);
        return Result.ok(result);
    }

    // ================== 密度聚类 ==================
    @Operation(
            summary = "密度聚类分析",
            description = """
                    ### 算法说明
                    基于DBSCAN算法对数据进行密度聚类：
                    - 自动识别核心点、边界点和噪声点
                    - 支持自定义邻域半径和最小点数
                    
                    ### 使用建议
                    1. 参数eps和minPoints对结果影响较大
                    2. 建议先进行降维处理以提高计算效率"""
    )
    @PostMapping("/density")
    public Result<DensityClusteringResult> densityClustering(@RequestBody @Parameter(description = "密度聚类请求参数") DensityClusteringRequest request) {
        DensityClusteringResult result = densityClusteringService.cluster(request);
        return Result.ok(result);
    }

    // ================== 关联度分析 ==================
    @Operation(
            summary = "关联度分析",
            description = """
                    ### 算法说明
                    基于相关系数矩阵对指标进行关联度分析：
                    - 支持Pearson、Spearman和Kendall三种相关系数
                    - 自动生成关联矩阵热图数据
                    
                    ### 使用建议
                    1. 对于非正态分布数据，建议使用Spearman或Kendall相关系数
                    2. 结果可用于特征选择和变量降维"""
    )
    @PostMapping("/correlation")
    public Result<CorrelationAnalysisResult> correlationAnalysis(@RequestBody @Parameter(description = "关联度分析请求参数") CorrelationAnalysisRequest request) {
        CorrelationAnalysisResult result = correlationAnalysisService.analyze(request);
        return Result.ok(result);
    }
}    